package com.ts.animal.controller;

import com.ts.animal.domain.CheckRecordDO;
import com.ts.animal.service.CheckRecordService;
import com.ts.common.annotation.Log;
import com.ts.common.utils.PageInfos;
import com.ts.common.utils.PageUtils;
import com.ts.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 检查记录表
 *
 * @author bobby
 * @email bobby@126.com
 * @date 2020-02-04 21:54:38
 */

@Controller
@RequestMapping("/animal/checkRecord")
public class CheckRecordController {
    @Autowired
    private CheckRecordService checkRecordService;

    @GetMapping()
    @RequiresPermissions("animal:checkRecord:checkRecord")
    String CheckRecord() {
        return "animal/checkRecord/checkRecord";
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("animal:checkRecord:checkRecord")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        PageUtils pageUtils = new PageInfos(params) {
            public List inject(Map params) {
                return checkRecordService.list(params);
            }
        }.query();
        return pageUtils;
    }


    @GetMapping("/add")
    @RequiresPermissions("animal:checkRecord:add")
    String add() {
        return "animal/checkRecord/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("animal:checkRecord:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        CheckRecordDO checkRecord = checkRecordService.get(id);
        model.addAttribute("checkRecord", checkRecord);
        return "animal/checkRecord/edit";
    }

    /**
     * 保存
     */
    @Log("添加检查记录")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("animal:checkRecord:add")
    public Result save(CheckRecordDO checkRecord) {
        if (checkRecordService.save(null, checkRecord) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 修改
     */
    @Log("修改检查记录")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("animal:checkRecord:edit")
    public Result update(CheckRecordDO checkRecord) {
        checkRecordService.update(null, checkRecord);
        return Result.ok();
    }

    /**
     * 删除
     */
    @Log("删除检查记录")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("animal:checkRecord:remove")
    public Result remove(Integer id) {
        if (checkRecordService.remove(id) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 删除
     */
    @Log("批量删除检查记录")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("animal:checkRecord:batchRemove")
    public Result remove(@RequestParam("ids[]") Integer[] ids) {
        checkRecordService.batchRemove(ids);
        return Result.ok();
    }

}
